//
//  YBscrollview.m
//  OurTourism
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "YBscrollview.h"
#import "Header.h"
#import "YBDestinationModel.h"

@interface YBscrollview ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scrollView;


@property (nonatomic, retain)NSMutableArray *imgViewArr;
@property (nonatomic, assign)NSInteger index; //记录所点轮播图
@property (nonatomic, retain)NSMutableArray *array;
@end


@implementation YBscrollview
- (instancetype)initWithFrame:(CGRect)frame dataArr:(NSMutableArray *)dataArr
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView:dataArr];
    }
    return self;
}
- (void)createView:(NSMutableArray *)dataArr
{
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    //偏移量
    self.scrollView.contentSize = CGSizeMake(WIDTH * (dataArr.count + 2), 0);
    self.scrollView.delegate = self;
    //默认显示图片
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    //整页滚动
    self.scrollView.pagingEnabled = YES;
    //隐藏滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    //边缘弹动效果 (快速拖动时 没有白边)
    self.scrollView.bounces = NO;
    
    
    self.array = dataArr;
    
    [self addSubview:self.scrollView];
    
    for (NSInteger i = 0; i < dataArr.count + 2; i++) {
        
        
        
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, 200)];
        
        img.tag = 2000 + i ;
        //设置手势前  打开用户交互
        img.userInteractionEnabled = YES;
        
        //图片加数据
        if (i == 0) {
            [img sd_setImageWithURL:[NSURL URLWithString:[dataArr[dataArr.count - 1] image_url]]];
            
        }else if (i > 0 && i < dataArr.count + 1) {
            [img sd_setImageWithURL:[NSURL URLWithString:[dataArr[i - 1] image_url]]];
        }
        else
        {
            [img sd_setImageWithURL:[NSURL URLWithString:[dataArr[0] image_url]]];
        }
        
        [self.scrollView addSubview:img];
        
        
        
        //给图片添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        
        
        [img addGestureRecognizer:tap];
     
    }
    
    
    
    //控制小圆点
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH/ 2  - 40, 170, WIDTH - 100, 20)];
    
    self.pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
    //页面总数
    self.pageControl.numberOfPages = dataArr.count;
    //开始页
    self.pageControl.currentPage = 0;
    
    
    
    //定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    [self addSubview:self.pageControl];
}


#pragma mark - tap手势
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 2000) {
        
        [self.delegate jumptoWebView:[self.array[self.array.count - 1] html_url]];
    }
    
    else if (tap.view.tag > 2000 && tap.view.tag < 2000 + self.array.count + 1)
    {
        
        [self.delegate jumptoWebView:[self.array[tap.view.tag - 2001] html_url]];
        
    }
    
    
    else
    {
        [self.delegate jumptoWebView:[self.array[0] html_url]];
    }
    
    
}- (void)autoScroll
{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0) animated:YES];
    //偏移到最后一张时 跳回应该显示的第一张
    if (self.scrollView.contentOffset.x / WIDTH == self.array.count + 1 ) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    
}

//将要开始拖拽时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //关闭定时器
    [self.timer invalidate];
}

//已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //开启定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4  target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    
    
    //偏移到最后一张时 跳回应该显示的第一张
    if (self.scrollView.contentOffset.x / WIDTH == self.array.count + 1 ) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    //偏移到第一张时 跳回应该显示的最后一张
    if (self.scrollView.contentOffset.x / WIDTH == 0) {
        self.scrollView.contentOffset = CGPointMake(WIDTH * self.array.count, 0);
    }
    
}
//手动拖拽轮播图
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.scrollView.contentOffset.x / WIDTH > 0 && self.scrollView.contentOffset.x / WIDTH <  self.array.count + 1 ) {
        NSInteger currentPage = self.scrollView.contentOffset.x / WIDTH ;
        self.pageControl.currentPage = currentPage - 1;
    }
    else if (self.scrollView.contentOffset.x/ WIDTH == 0)
    {
        self.pageControl.currentPage = self.array.count - 1;
        
    }
    else if (self.scrollView.contentOffset.x /WIDTH == self.array.count + 1)
    {
        self.pageControl.currentPage = 0;
        
    }
}

- (void)reload
{
    self.scrollView.contentOffset = CGPointZero;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
